package com.hlkj.pay.controller.openapi;

import cn.hutool.json.JSONObject;
import com.hlkj.pay.app.order.IPayOrderAppService;
import com.hlkj.pay.infrastructure.model.common.CommonAppDO;
import com.hlkj.pay.infrastructure.model.order.PayOrderDO;
import com.hlkj.pay.infrastructure.model.pay.ProviderChannelConfigDO;
import com.hlkj.pay.service.common.ICommonAppConfigService;
import com.hlkj.pay.service.order.IPayOrderService;
import com.hlkj.pay.service.pay.IProviderService;
import com.hlkj.pay.util.AlipayOAuthUtils;
import com.hlkj.pay.util.WeChatOAuthUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 废弃 暂时不用
 */
@Slf4j
@RestController
@RequestMapping("/openapi/oauth")
@RequiredArgsConstructor
public class ThirdAuthController {
    private final IPayOrderService payOrderService;

    private final IProviderService providerService;

    private final ICommonAppConfigService commonAppConfigService;

    @GetMapping(value = "/wechat/auth", produces = "text/html;charset=GBK")
    public String wechatAuth(HttpServletRequest request) {
        JSONObject ret = new JSONObject();
        try {
            String redirectUrl = request.getParameterMap().get("redirectUrl")[0];

            PayOrderDO payOrderDO= payOrderService.queryPayOrder("");
            ProviderChannelConfigDO providerChannelConfigDO = providerService.queryConfig(payOrderDO.getProviderCode(), payOrderDO.getChannelCode());
            CommonAppDO commonAppDO = commonAppConfigService.detail(providerChannelConfigDO.getAlipayPubId());

            String url = WeChatOAuthUtils.getAuthCode("wx264bba4e9b29efe8", redirectUrl);
            log.info("当前地址为 => " + redirectUrl);
            ret.set("url", url);
            ret.set("code", 200);
        } catch (Exception e) {
            ret.set("msg", e.getMessage());
            ret.set("code", 999999);
        }
        return ret.toString();
    }

    @GetMapping(value = "/alipay/auth", produces = "text/html;charset=GBK")
    public String alipayAuth(HttpServletRequest request) {
        JSONObject ret = new JSONObject();
        try {
            String redirectUrl = request.getParameterMap().get("redirectUrl")[0];
            String url = AlipayOAuthUtils.getAuthCode("2017102109443567", redirectUrl);
            log.info("当前地址为 => " + redirectUrl);
            ret.set("url", url);
            ret.set("code", 200);
        } catch (Exception e) {
            ret.set("msg", e.getMessage());
            ret.set("code", 999999);
        }
        return ret.toString();
    }
}
